package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.ReturnUUID;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.domain.SjProjectReust;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.gcgl.service.ISjProjectReustService;

/**
 * 项目成果管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Controller
@RequestMapping("/sj/reust")
public class SjProjectReustController extends BaseController
{
    private String prefix = "sj/reust";

    @Autowired
    private ISjProjectReustService sjProjectReustService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;

    @RequiresPermissions("gcgl:reust:view")
    @GetMapping()
    public String reust()
    {
        return prefix + "/reust";
    }

    /**
     * 查询项目成果管理列表
     */
    @RequiresPermissions("gcgl:reust:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjProjectReust sjProjectReust)
    {
        startPage();
        sjProjectReust.setDeleteFlag(1);
        List<SjProjectReust> list = sjProjectReustService.selectSjProjectReustList(sjProjectReust);
        return getDataTable(list);
    }

    /**
     * 导出项目成果管理列表
     */
    @RequiresPermissions("gcgl:reust:export")
    @Log(title = "项目成果管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjProjectReust sjProjectReust)
    {
        List<SjProjectReust> list = sjProjectReustService.selectSjProjectReustList(sjProjectReust);
        ExcelUtil<SjProjectReust> util = new ExcelUtil<SjProjectReust>(SjProjectReust.class);
        return util.exportExcel(list, "项目成果管理数据");
    }

    /**
     * 新增项目成果管理
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return prefix + "/add";
    }

    /**
     * 新增保存项目成果管理
     */
    @RequiresPermissions("gcgl:reust:add")
    @Log(title = "项目成果管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(SjProjectReust sjProjectReust)
    {
    	sjProjectReust.setOrderNo(ReturnUUID.getOrderNo("CG"));
    	sjProjectReust.setDeleteFlag(1);
    	sjProjectReust.setCreateUser(getLoginName());
    	sjProjectReust.setCreateAt(new Date());
    	sjProjectReust.setUpdateUser(getLoginName());
    	sjProjectReust.setUpdateAt(new Date());
        return toAjax(sjProjectReustService.insertSjProjectReust(sjProjectReust));
    }

    /**
     * 修改项目成果管理
     */
    @RequiresPermissions("gcgl:reust:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjProjectReust sjProjectReust = sjProjectReustService.selectSjProjectReustById(id);
        
        mmap.put("wenjvo", sjProjectReust.getWenj());
        if(sjProjectReust.getWenj()!=null && !sjProjectReust.getWenj().equals("")){
        	String[] zlis = sjProjectReust.getWenj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectReust.setWenj(file);
        }
        
        mmap.put("sjProjectReust", sjProjectReust);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目成果管理
     */
    @RequiresPermissions("gcgl:reust:edit")
    @Log(title = "项目成果管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjProjectReust sjProjectReust)
    {
    	sjProjectReust.setUpdateUser(getLoginName());
    	sjProjectReust.setUpdateAt(new Date());
        return toAjax(sjProjectReustService.updateSjProjectReust(sjProjectReust));
    }

    /**
     * 删除项目成果管理
     */
    @RequiresPermissions("gcgl:reust:remove")
    @Log(title = "项目成果管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjProjectReustService.deleteSjProjectReustByIds(ids));
    }
    
    @GetMapping("/p4reust/{id}")
    public String p4reust(ModelMap mmap,@PathVariable("id") Long id)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailReust";
    }

    /**
     * 查询项目成果管理列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(SjProjectReust sjProjectReust)
    {
    	startPage();
        sjProjectReust.setDeleteFlag(1);
        List<SjProjectReust> list = sjProjectReustService.selectSjProjectReustList(sjProjectReust);
        
        for(SjProjectReust ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        return getDataTable(list);
    }
    
    
    @GetMapping("/p4reustYs/{id}")
    public String p4reustYs(ModelMap mmap,@PathVariable("id") Long id)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailReustYs";
    }

    /**
     * 查询项目成果管理列表
     */
    @PostMapping("/p4listYs")
    @ResponseBody
    public TableDataInfo p4listYs(SjProjectReust sjProjectReust){
        sjProjectReust.setDeleteFlag(1);
        sjProjectReust.setType("13");//验收
        List<SjProjectReust> list = sjProjectReustService.selectSjProjectReustList(sjProjectReust);
        
        for(SjProjectReust ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        return getDataTable(list);
    }
    
    /**
     * 新增项目成果管理
     */
    @GetMapping("/addYs/{id}")
    public String addYs(@PathVariable("id") Long id, ModelMap mmap)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return prefix + "/addYs";
    }
    
    /**
     * 修改项目成果管理
     */
    @GetMapping("/editYs/{id}")
    public String editYs(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjProjectReust sjProjectReust = sjProjectReustService.selectSjProjectReustById(id);
        
        mmap.put("wenjvo", sjProjectReust.getWenj());
        if(sjProjectReust.getWenj()!=null && !sjProjectReust.getWenj().equals("")){
        	String[] zlis = sjProjectReust.getWenj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjProjectReust.setWenj(file);
        }
        
        mmap.put("sjProjectReust", sjProjectReust);
        return prefix + "/editYs";
    }
}